package com.shouhuobao.take.weight.datepicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.shouhuobao.station.R;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by joker on 2017/9/16.
 * 选择时间 00:00
 */
public class TimePicker extends LinearLayout implements NumberPicker.OnValueChangeListener {

    private NumberPicker mHourPicker;
    private NumberPicker mMinutePicker;

    private Calendar mCalendar;

    private OnDateChangedListener mOnDateChangedListener;

    private LayoutInflater mLayoutInflater;

    public TimePicker(Context context) {
        this(context, null);
    }

    public TimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        init();
    }

    private void init() {
        mLayoutInflater.inflate(R.layout.layout_time_picker, this, true);
        mHourPicker = (NumberPicker) findViewById(R.id.hour_picker);
        mMinutePicker = (NumberPicker) findViewById(R.id.minute_picker);

        mHourPicker.setOnValueChangeListener(this);
        mMinutePicker.setOnValueChangeListener(this);

        String[] hourNames = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
        String[] minuteNames = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
                "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
                "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
        mHourPicker.setCustomTextArray(hourNames);
        mMinutePicker.setCustomTextArray(minuteNames);

        mCalendar = Calendar.getInstance();
        setDate(mCalendar.getTime());
    }

    public TimePicker setDate(Date date) {
        mCalendar.setTime(date);
        mHourPicker.setCurrentNumber(mCalendar.get(Calendar.HOUR_OF_DAY));
        mMinutePicker.setCurrentNumber(mCalendar.get(Calendar.MINUTE));
        return this;
    }

    @Override
    public void onValueChange(final NumberPicker picker, final int oldVal, final int newVal) {

        if (picker == mHourPicker) {
            mCalendar.set(Calendar.HOUR_OF_DAY, newVal);
        } else if (picker == mMinutePicker) {
            mCalendar.set(Calendar.MINUTE, newVal);
        }
        notifyDateChanged();
    }

    /**
     * The callback used to indicate the user changes\d the date.
     */
    public interface OnDateChangedListener {

        /**
         * Called upon a date change.
         *
         * @param view   The view associated with this listener.
         * @param hour   The hour that was set.
         * @param minute The minute that was set (0-59) for compatibility
         *               with {@link Calendar}.
         */
        void onDateChanged(TimePicker view, int hour, int minute);
    }

    public TimePicker setOnDateChangedListener(OnDateChangedListener l) {
        mOnDateChangedListener = l;
        return this;
    }

    private void notifyDateChanged() {
        if (mOnDateChangedListener != null) {
            mOnDateChangedListener.onDateChanged(this, getHour(), getMinute());
        }
    }

    public int getHour() {
        return mCalendar.get(Calendar.HOUR_OF_DAY);
    }

    public int getMinute() {
        return mCalendar.get(Calendar.MINUTE);
    }

    @Override
    public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
        super.setSoundEffectsEnabled(soundEffectsEnabled);
        mHourPicker.setSoundEffectsEnabled(soundEffectsEnabled);
        mMinutePicker.setSoundEffectsEnabled(soundEffectsEnabled);
    }

    public TimePicker setRowNumber(int rowNumber) {
        mHourPicker.setRowNumber(rowNumber);
        mMinutePicker.setRowNumber(rowNumber);
        return this;
    }

    public TimePicker setTextSize(float textSize) {
        mHourPicker.setTextSize(textSize);
        mMinutePicker.setTextSize(textSize);
        return this;
    }

    public TimePicker setFlagTextSize(float textSize) {
        mHourPicker.setFlagTextSize(textSize);
        mMinutePicker.setFlagTextSize(textSize);
        return this;
    }

    public TimePicker setTextColor(int color) {
        mHourPicker.setTextColor(color);
        mMinutePicker.setTextColor(color);
        return this;
    }

    public TimePicker setFlagTextColor(int color) {
        mHourPicker.setFlagTextColor(color);
        mMinutePicker.setFlagTextColor(color);
        return this;
    }

    public TimePicker setBackground(int color) {
        super.setBackgroundColor(color);
        mHourPicker.setBackground(color);
        mMinutePicker.setBackground(color);
        return this;
    }
}
